package com.example.zg.androidnewview.behaviors;

import android.content.Context;
import android.graphics.Rect;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.view.WindowInsetsCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

/**
 * 创建日期:2019/1/10 on 11:11
 * 描述：
 * 作者: zhougan
 */
//其中有一个泛型，它的作用是指定要使用这个Behavior的View的类型，
// 可以是Button、TextView等等。如果希望所有的View都可以使用则指定泛型为View即可
public class TextViewBehavior extends CoordinatorLayout.Behavior<TextView> {
    private static final String TAG = "TextViewBehavior";
    public TextViewBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    //确定使用Behavior的View要依赖的View的类
    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, TextView child, View dependency) {
        return dependency instanceof Snackbar.SnackbarLayout;

    }

    //当被依赖的View状态改变时回调
    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, TextView child, View dependency) {
        Log.i(TAG, "getTranslationY: "+dependency.getTranslationY());
        Log.i(TAG, "getHeight: "+dependency.getHeight());
        float y=dependency.getTranslationY()-dependency.getHeight();

        child.setTranslationY(y);

        return true;
    }

    //当被依赖的View移除时回调
    @Override
    public void onDependentViewRemoved(CoordinatorLayout parent, TextView child, View dependency) {
        super.onDependentViewRemoved(parent, child, dependency);
    }

    //测量使用Behavior的View尺寸
    @Override
    public boolean onMeasureChild(CoordinatorLayout parent, TextView child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {
        return super.onMeasureChild(parent, child, parentWidthMeasureSpec, widthUsed, parentHeightMeasureSpec, heightUsed);
    }

    //确定使用Behavior的View位置
    @Override
    public boolean onLayoutChild(CoordinatorLayout parent, TextView child, int layoutDirection) {
        return super.onLayoutChild(parent, child, layoutDirection);
    }

    //嵌套滑动开始（ACTION_DOWN），确定Behavior是否要监听此次事件
    @Override
    public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull TextView child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
        return super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, axes, type);
    }

    @Override
    public void onNestedScrollAccepted(@NonNull CoordinatorLayout coordinatorLayout, @NonNull TextView child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
        super.onNestedScrollAccepted(coordinatorLayout, child, directTargetChild, target, axes, type);
    }

    //嵌套滑动结束（ACTION_UP或ACTION_CANCEL）
    @Override
    public void onStopNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull TextView child, @NonNull View target, int type) {
        super.onStopNestedScroll(coordinatorLayout, child, target, type);
    }

    //嵌套滑动进行中，要监听的子 View的滑动事件已经被消费
    @Override
    public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull TextView child, @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type);
    }

    //嵌套滑动进行中，要监听的子 View将要滑动，滑动事件即将被消费（但最终被谁消费，可以通过代码控制）
    @Override
    public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull TextView child, @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
    }

    //要监听的子 View在快速滑动中
    @Override
    public boolean onNestedFling(@NonNull CoordinatorLayout coordinatorLayout, @NonNull TextView child, @NonNull View target, float velocityX, float velocityY, boolean consumed) {
        return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
    }

    //要监听的子View即将快速滑动
    @Override
    public boolean onNestedPreFling(@NonNull CoordinatorLayout coordinatorLayout, @NonNull TextView child, @NonNull View target, float velocityX, float velocityY) {
        return super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY);
    }


}
